<?php
namespace app\common\model;
use think\Model;
use think\Session;
/**
 * 实名认证
 */
class Authentication extends Model
{
	// 接口说明
	// 接口地址：http://api.id98.cn/api/idcard
	// 请求方式：get/post
	// 返回格式：json/xml
	// 编码类型：utf-8 
	// 接口返回参数
	// {
	// "isok":1,
	// "code":1,
	// "data":
	// {"err":0,
	// "address":"\u5c71\u4e1c\u7701\u6cf0\u5b89\u5e02\u5cb1\u5cb3\u533a",
	// "sex":"M",
	// "birthday":"1986-02-12"}
	// }
	private $url = 'http://api.id98.cn/api/idcard';
    private $parameter = '';
    private $appkey = '94b026f08aaa8897ec9c096205c6959a';
	/**
	 * [CheckPhoneCode 校验身份证]
	 * @param [type] $loc_code [description]
	 * @param [type] $pho_code [description]
	 */
	public function Check($user_id,$name,$ID_number,$method='post')
	{
		//检测用户是否实名认证通过
		$authentication = db('users')->where('id',$user_id)->value('authentication');
		if ($authentication) {
			return $this->success('该用户已经实名认证');
		}
		$res = ['isok'=>1,'code'=>'','messsage'=>''];
		$this->parameter = array(
                'appkey' => $this->appkey,
                'name' => $name,
                'cardno' => $ID_number,
        );
		if ($method == 'post') {
            $r = \services\Http::post($this->url, $this->parameter);
        } else {
            $r = \services\Http::get($this->url, $this->parameter);
        }
        $result = json_decode($r);
        $code = $result->code;
        $isok = $result->isok;

        $res = ['isok'=>$isok,'code'=>$code ,'messsage'=>$this->getCodeMean($code)];
        return $res;

        //其他
        if ($isok==1&&$code==1) {
        	//身份一致
        	$res = ['isok'=>$isok,'code'=>$code ,'messsage'=>$this->getCodeMean($code)];
        	return $res;
        }else{
        	//身份不一致
        	$res = ['isok'=>$isok,'code'=>$code ,'messsage'=>$this->getCodeMean($code)];
        	return $res;
        }
	}
	/**
	 * [getCodeMean code含义]
	 * @param string $value [description]
	 */
	public function getCodeMean($value)
	{
		$code= ['1'=>'一致','2'=>'不一致','3'=>'无此身份证号码','11'=>'参数不正确','12'=>'商户余额不足','13'=>'appkey不存在','14'=>'IP被拒绝','20'=>'身份证中心维护中'];
        return $code[$value];
	}
}